package edu.utn.tacs.grupouno.ui.client;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DatePicker;

import edu.utn.tacs.grupouno.ui.client.dto.TurnoDTO;

public class TurnoSelectionPanel extends VerticalPanel {
	
	private TurnoActionExecutor executor;

	public TurnoSelectionPanel() {
		initComponents();
	}

	public TurnoSelectionPanel(TurnoActionExecutor executor) {
		this.executor = executor;
		initComponents();
	}
	
	private void initComponents() {
		HorizontalPanel container = new HorizontalPanel();
		container.setSpacing(5);
		final VerticalPanel turnosPanel = new VerticalPanel();
		final TurnoUIServiceAsync turnoService = GWT.create(TurnoUIService.class);

		final AsyncCallback<List<TurnoDTO>> callback = new AsyncCallback<List<TurnoDTO>>() {
			public void onFailure(Throwable caught) {
				turnosPanel.clear();
				turnosPanel.add(new Label(caught.getMessage()));
			}
			@Override
			public void onSuccess(List<TurnoDTO> turnos) {
				turnosPanel.clear();
				Map<String, Widget> map = new HashMap<String, Widget>();
				for(TurnoDTO turno : turnos) {
					map.put(turno.getHorario(), new TurnoDetailsPanel(turno, executor));
				}
				
				for(int i = 8; i < 18; i++) {
					Panel row = new HorizontalPanel();
					Label hour = new Label(String.valueOf(i) + ":");
					hour.setStyleName("bigNumber");
					
					Panel rightPanel = new VerticalPanel();
					
					for(String h : new String[]{"00", "30"}) {
						String key = i + ":" + h;
						if(map.containsKey(key)) {
							rightPanel.add(map.get(key));
						} else {
							rightPanel.add(getEmptyPanel(h));
						}
					}
					
					row.add(hour);
					row.add(rightPanel);
					Panel decorator = new DecoratorPanel();
					decorator.setStyleName("itemRow");
					decorator.setWidth("15em");
					decorator.add(row);
					turnosPanel.add(decorator);
				}
				
			}
			private Widget getEmptyPanel(String string) {
				Panel panel = new HorizontalPanel();
				panel.add(new Label(string));
				panel.setSize("15em", "1em");
				panel.setStyleName("emptyCell");
				return panel;
			}
		};
		
		DatePicker datePicker = new DatePicker();
		datePicker.addValueChangeHandler(new ValueChangeHandler<Date>() {
			public void onValueChange(ValueChangeEvent<Date> event) {
				turnosPanel.clear();
				Date date = event.getValue();
				String dateString = DateUtil.toString(date);
				turnosPanel.add(new Label("Cargando... " + dateString));
				turnoService.getTurnos(dateString, callback);
			}
		});

		datePicker.setValue(new Date(), true);

		Button addNewButton = new Button("Nuevo Turno Semanal", new ClickHandler() {
			public void onClick(ClickEvent sender) {
				DialogBox dialogBox = createDialogBox();
				dialogBox.center();
				dialogBox.show();
			}
		});
		
		VerticalPanel leftPanel = new VerticalPanel();
		leftPanel.add(datePicker);
		leftPanel.add(addNewButton);
		
		container.add(leftPanel);
		container.add(turnosPanel);
		add(container);
	}

	private DialogBox createDialogBox() {
		final DialogBox box = new DialogBox();
		box.setText("Nuevo turno semanal");
		Button closeButton = new Button("Cancelar", new ClickHandler() {
			public void onClick(ClickEvent event) {
				box.hide();
			}
		});
		VerticalPanel container = new VerticalPanel();
		container.add(new TurnoSemanalCreationPanel(box));
		container.add(closeButton);
		box.setGlassEnabled(true);
		box.setAnimationEnabled(true);
		box.add(container);
		return box;
	}

}
